package org.jeecg.modules.demo.sysuser.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.sysuser.entity.SysUser;
import org.jeecg.modules.demo.sysuser.entity.VEcpUser;
import org.jeecg.modules.demo.sysuser.service.ISysUserService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.sysuser.service.IVEcpUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: sys_user
 * @Author: jeecg-boot
 * @Date:   2025-04-27
 * @Version: V1.0
 */
@Api(tags="sys_user")
@RestController
@RequestMapping("/sysuser/sysOldUser")
@Slf4j
public class SysUserOldController extends JeecgController<SysUser, ISysUserService> {
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IVEcpUserService userService;

	/**
	 * 分页列表查询
	 *
	 * @param sysUser
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "sys_user-分页列表查询")
	@ApiOperation(value="sys_user-分页列表查询", notes="sys_user-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SysUser>> queryPageList(SysUser sysUser,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysUser> queryWrapper = QueryGenerator.initQueryWrapper(sysUser, req.getParameterMap());
		Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
		IPage<SysUser> pageList = sysUserService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @ApiOperation(value="v_ecp_user-分页列表查询", notes="v_ecp_user-分页列表查询")
	 @GetMapping(value = "/ecpuserlist")
	 public Result<IPage<VEcpUser>> queryPageList(VEcpUser vEcpUser,
												  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												  HttpServletRequest req) {
		 QueryWrapper<VEcpUser> queryWrapper = QueryGenerator.initQueryWrapper(vEcpUser, req.getParameterMap());
		 Page<VEcpUser> page = new Page<VEcpUser>(pageNo, pageSize);
		 IPage<VEcpUser> pageList = userService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	/**
	 *   添加
	 *
	 * @param sysUser
	 * @return
	 */
	@AutoLog(value = "sys_user-添加")
	@ApiOperation(value="sys_user-添加", notes="sys_user-添加")
	//@RequiresPermissions("sysuser:sys_user:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SysUser sysUser) {
		String id = sysUserService.genrateSysUserId("SYS_USER");
		sysUser.setUserid(Integer.parseInt(id));
		sysUser.setSysid(Integer.parseInt(id));
		sysUser.setPassword("E780A11E3B3D86BB4BE93B6DBF885196");
		sysUserService.save(sysUser);
		return Result.OK("添加成功！");
	}



	/**
	 *  编辑
	 *
	 * @param sysUser
	 * @return
	 */
	@AutoLog(value = "sys_user-编辑")
	@ApiOperation(value="sys_user-编辑", notes="sys_user-编辑")
	//@RequiresPermissions("sysuser:sys_user:edit")

	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SysUser sysUser) {
		System.out.println(sysUserService.updateById(sysUser));
		return Result.OK("操作成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "sys_user-通过id删除")
	@ApiOperation(value="sys_user-通过id删除", notes="sys_user-通过id删除")
	//@RequiresPermissions("sysuser:sys_user:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		sysUserService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "sys_user-批量删除")
	@ApiOperation(value="sys_user-批量删除", notes="sys_user-批量删除")
	//@RequiresPermissions("sysuser:sys_user:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysUserService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "sys_user-通过id查询")
	@ApiOperation(value="sys_user-通过id查询", notes="sys_user-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SysUser> queryById(@RequestParam(name="id",required=true) String id) {
		SysUser sysUser = sysUserService.getById(id);
		if(sysUser==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sysUser);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sysUser
    */
    //@RequiresPermissions("sysuser:sys_user:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysUser sysUser) {
        return super.exportXls(request, sysUser, SysUser.class, "sys_user");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("sysuser:sys_user:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysUser.class);
    }

}
